package com.iflyings.android.util

import android.annotation.SuppressLint
import android.content.Context
import android.os.StatFs
import android.os.storage.StorageManager
import java.lang.reflect.InvocationTargetException
import java.util.*

object StorageUtils {

    @SuppressLint("PrivateApi", "DiscouragedPrivateApi", "SoonBlockedPrivateApi")
    fun getVolumesInfo(context: Context): List<Map<String, String?>> {
        val list = ArrayList<Map<String, String?>>()
        val storageManager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
        try {
            val methodGetVolumes = StorageManager::class.java.getDeclaredMethod("getVolumes")
            val volumesInfo = methodGetVolumes.invoke(storageManager) as List<*>?
            val volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo")
            val getDiskIdMethod = volumeInfoClazz.getDeclaredMethod("getDiskId")
            val fieldFsType = volumeInfoClazz.getDeclaredField("fsType")
            val fieldFsUuid = volumeInfoClazz.getDeclaredField("fsUuid")
            val fieldInternalPath = volumeInfoClazz.getDeclaredField("internalPath")
            val fieldFsLabel = volumeInfoClazz.getDeclaredField("fsLabel")
            val fieldPath = volumeInfoClazz.getDeclaredField("path")
            val fieldType = volumeInfoClazz.getDeclaredField("type")
            if (volumesInfo != null) {
                for (volumeInfo in volumesInfo) {
                    if (volumeInfo != null) {
                        val diskId = getDiskIdMethod.invoke(volumeInfo) as String?
                        val fsType = fieldFsType.get(volumeInfo) as String?
                        val fsUuid = fieldFsUuid.get(volumeInfo) as String?
                        val internalPath = fieldInternalPath.get(volumeInfo) as String?
                        val fsLabel = fieldFsLabel.get(volumeInfo) as String?
                        val path = fieldPath.get(volumeInfo) as String?
                        val type = fieldType.get(volumeInfo) as Int
                        if (path == null || internalPath == null) {
                            continue
                        }
                        HashMap<String, String?>().apply {
                            put("diskId", diskId)
                            put("fsType", fsType)
                            put("fsUuid", fsUuid)
                            put("internalPath", internalPath)
                            put("fsLabel", fsLabel)
                            put("path", path)
                            put("type", type.toString())
                            list.add(this)
                        }
                    }
                }
            }
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        }
        return list
    }

    fun getStorageAvailableSize(path: String): Long {
        return StatFs(path).availableBytes
    }

    fun getStorageTotalSize(path: String): Long {
        return StatFs(path).totalBytes
    }


    /*
    public static boolean unmountVolume(String path) {
        IStorageManager service = getStorageManager();
        if (service == null) {
            return false;
        }
        try {
            service.unmountVolume(path, true, false);
            return true;
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean formatVolume(String path) {
        IStorageManager service = getStorageManager();
        if (service == null) {
            return false;
        }
        try {
            LogHelper.d(TAG, "doFormat:" + path);
            service.formatVolume(path);
            LogHelper.d(TAG, "doFormat: done");
            return true;
        } catch (RemoteException e) {
            LogHelper.d(TAG, "doFormat: error");
        }
        return false;
    }
    */

}